<?php

namespace Wgx\Admin\Entity;

use PhpYes\Apps;
use PhpYes\Entity\BaseEntity;
use Wgx\PdoEx\QueryBuilder;

class UserEntity extends BaseEntity
{
    const GenderUnknown = 0;
    const GenderMale = 1;
    const GenderFemale = 2;

    /**
     * @param string $password
     * @return string
     */
    public static function hashPassword($password)
    {
        $password = empty($password) ? '' : trim($password);

        return md5($password);
    }

    public function __construct($data = null)
    {
        if (empty($data)) {
            $data = array();
        }
        if (empty($data['create_dt'])) {
            $data['create_dt'] = (new \DateTime())->format(QueryBuilder::SqlDateTimeFormat);
        }
        if (empty($data['password'])) {
            $data['password'] = '';
        }
        if (empty($data['nickname'])) {
            $data['nickname'] = '小白' . time();
        }
        if (!isset($data['gender'])) {
            $data['gender'] = self::GenderUnknown;
        }

        parent::__construct($data);
    }

    /**
     * @inheritDoc
     */
    public function getConnName()
    {
        return App::$p->userDb()->getConnName();
    }

    /**
     * @inheritDoc
     */
    public function getDb($quoted = true)
    {
        return App::$p->userDb()->getDb($quoted);
    }

    /**
     * @inheritDoc
     */
    public function getTable($quoted = true)
    {
        if ($quoted) {
            return '`user`';
        } else {
            return 'user';
        }
    }

    public function formatNormal()
    {
        return $this->format(array(
            'create_dt' => 'createDt',
            'password' => false,
        ));
    }

    /**
     * 不走缓存，直接从数据库取数据
     *
     * @param string $phone - 手机号码
     * @return UserEntity|null
     */
    public function findByPhone($phone)
    {
        $phone = empty($phone) ? null : trim($phone);
        if (empty($phone)) {
            return null;
        }

        // 不走缓存
        $result = QueryBuilder::useDb($this->getConnName())->select($this->getTable(), '*', array('phone' => $phone))->getPdoStatement()->fetchAll(\PDO::FETCH_ASSOC);
        if (empty($result)) {
            return null;
        }
        return new UserEntity($result[0]);
    }

    /**
     * 不走缓存，直接从数据库取数据
     *
     * @param string $email - 电子邮箱
     * @return UserEntity|null
     */
    public function findByEmail($email)
    {
        $email = empty($email) ? null : trim($email);
        if (empty($email)) {
            return null;
        }

        // 不走缓存
        return $this->selectFirst(['email' => $email]);
    }

    /**
     * @return string
     */
    public function getCreateDt()
    {
        return $this->get_('create_dt');
    }

    /**
     * @param string $createDt
     */
    public function setCreateDt($createDt)
    {
        $this->set_('create_dt', $createDt, self::DataTypeString);
    }

    /**
     * @return string
     */
    public function getPhone()
    {
        return $this->get_('phone');
    }

    /**
     * @param string $phone
     */
    public function setPhone($phone)
    {
        $this->set_('phone', $phone, self::DataTypeString);
    }

    /**
     * @return string
     */
    public function getEmail()
    {
        return $this->get_('email');
    }

    /**
     * @param string $email
     */
    public function setEmail($email)
    {
        $this->set_('email', $email, self::DataTypeString);
    }

    /**
     * @return string
     */
    public function getPassword()
    {
        return $this->get_('password');
    }

    /**
     * @param string $password
     */
    public function setPassword($password)
    {
        $this->set_('password', $password, self::DataTypeString);
    }

    /**
     * @return string
     */
    public function getNickname()
    {
        return $this->get_('nickname');
    }

    /**
     * @param string $nickname
     */
    public function setNickname($nickname)
    {
        $this->set_('nickname', $nickname, self::DataTypeString);
    }

    /**
     * @return int
     */
    public function getGender()
    {
        return $this->get_('gender');
    }

    /**
     * @param int $gender
     */
    public function setGender($gender)
    {
        $this->set_('gender', $gender, self::DataTypeInt);
    }

    /**
     * @return string
     */
    public function getAvatar()
    {
        return $this->get_('avatar');
    }

    /**
     * @param string $avatar
     */
    public function setAvatar($avatar)
    {
        $this->set_('avatar', $avatar, self::DataTypeString);
    }
}